package com.yfp.client.controller;

import com.github.pagehelper.util.StringUtil;
import com.yfp.client.annotation.AnonymousAccess;
import com.yfp.client.domain.vo.ClientMachineFavoritesVo;
import com.yfp.client.domain.vo.MachineComboVO;
import com.yfp.client.domain.vo.MachineVo;
import com.yfp.client.domain.vo.MarCouponPackShopVO;
import com.yfp.client.mapper.ClientMapper;
import com.yfp.client.service.ClientMachineFavoritesService;
import com.yfp.client.service.MachineService;
import com.yfp.client.service.OrderComboService;
import com.yfp.client.service.OrderService;
import com.yfp.client.util.Response;
import com.yfp.common.core.web.controller.BaseController;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 设备Controller
 *
 * @CreateDate: 2024/5/24 10:25
 * @UpdateUser:
 * @UpdateDate: 2024/5/24 10:25
 * @UpdateRemark: 暂无修改
 * @Version: 1.0
 */
@RestController
@RequestMapping("/client/machine")
@Tag(name = "设备模块")
public class MachineController extends BaseController {
    @Autowired
    private MachineService machineService;
    @Autowired
    private OrderComboService orderComboService;
    @Autowired
    private ClientMachineFavoritesService clientMachineFavoritesService;
    @Resource
    private OrderService orderService;
    @Resource
    private ClientMapper clientMapper;

    @Operation(summary = "查询设备列表")
    @GetMapping("")
    @AnonymousAccess
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK"),
    })
    @ApiOperation(value = "查询设备列表", notes = "查询设备列表", response = MachineVo.class)
    public Response<List<MachineVo>> findClientMachineFavorites(@ApiParam("当前客户ID") String clientId,
                                                                @ApiParam("纬度") String latitude,
                                                                @ApiParam("经度") String longitude,
                                                                @ApiParam("设备名称") String machineName,
                                                                @ApiParam("设备ID") String machineId,
                                                                @ApiParam("分页条数") Integer pageSize,
                                                                @ApiParam("分页数") Integer pageNum) {
        if (pageSize > 3) {
            startPage();
        }
        List<MachineVo> machines = machineService.findMachines(clientId, latitude, longitude, machineName, machineId);

        // 如果不存在订单号，则打上距离最近的设备点位标签
        String orderId = orderComboService.getByUserId(clientId);
        if (StringUtil.isEmpty(orderId)) {
            // 获取最近的设备点位id
            MachineVo machineVo = machineService.shortDistancePositionId(latitude,longitude);
            if(null != machineVo){
                clientMapper.updatePositionId(clientId, machineVo.getPositionId());
            }
        }

        List<MachineVo> resultMachines = new ArrayList<>();
        if (StringUtils.hasLength(clientId) && !CollectionUtils.isEmpty(machines)) {
            // 查询订单设备
            String orderMachineId = orderComboService.countMachineId(clientId);
            List<ClientMachineFavoritesVo> clientMachineFavorites =
                    clientMachineFavoritesService.findClientMachineFavorites(clientId, latitude, longitude);
            List<String> filterMachineIds = clientMachineFavorites.stream().map(s -> s.getMachineId()).collect(Collectors.toList());
            // 先排下单后排收藏后再最近
            for (MachineVo machine : machines) {
                if (machine.getMachineId().equals(orderMachineId)) {
                    machine.setType(0);
                    resultMachines.add(machine);
                    break;
                }
            }

            for (MachineVo machine : machines) {
                if (!machine.getMachineId().equals(orderMachineId) && filterMachineIds.contains(machine.getMachineId())) {
                    machine.setType(1);
                    resultMachines.add(machine);
                }
            }

            filterMachineIds.add(orderMachineId);

            for (MachineVo machine : machines) {
                if (!filterMachineIds.contains(machine.getMachineId())) {
                    resultMachines.add(machine);
                }
            }
        } else {
            resultMachines.addAll(machines);
        }

        for (MachineVo machine : resultMachines) {
            if (null == machine.getType()) {
                machine.setType(2);
                break;
            }
        }

        for (MachineVo resultMachine : resultMachines) {
            String machineId1 = resultMachine.getMachineId();
            Integer awaitNum = orderService.getAwaitNum(machineId1);
            resultMachine.setAwaitNum(awaitNum);
        }

        if (pageSize <= 3 && !CollectionUtils.isEmpty(resultMachines) && resultMachines.size() > 3) {
            return Response.success(resultMachines.subList(0, 3));
        }
        return Response.success(resultMachines);
    }

    @GetMapping("/combolist")
    @Operation(summary = "设备套餐列表", description = "套餐列表页面使用")
    @AnonymousAccess
    public Response<MachineComboVO> machineComboList(String machineId, Integer pageIndex, Integer pageSize) {
        startPage();
        MachineComboVO machineComboVO = machineService.machineComboList(machineId);
        return Response.success(machineComboVO);
    }

    @GetMapping("/couponPack-list")
    @Operation(summary = "礼包套餐列表", description = "吃饭套餐列表页面使用")
    @AnonymousAccess
    public Response<List<MarCouponPackShopVO>> couponPackList(@RequestParam(value = "cityName", required = false) String cityName) {
        List<MarCouponPackShopVO> packShopVOList = machineService.couponPackList(cityName);
        return Response.success(packShopVOList);
    }

    @GetMapping("/findMarAssociationQrCode")
    @Operation(summary = "根据设备ID查询社群俄二维码", description = "套餐列表页面使用")
    @AnonymousAccess
    public Response<String> findMarAssociationQrCode(String machineId) {
        return Response.success(machineService.findMarAssociationQrCode(machineId));
    }

}